﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct ListNode
{
	int val;
	struct ListNode* next;

}LN;

//创造结构体
LN* NewLN(int x)
{
	LN* newln = (LN*)malloc(sizeof(LN));
	if (newln == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newln->val = x;
	newln->next = NULL;
	return newln;
}
//尾插
void Pushback(LN** pphead, int x)
{
	assert(pphead);
	LN* newln = NewLN(x);
	//如果是空链表
	if (*pphead == NULL)
	{
		*pphead = newln;
	}
	else //不是空链表
	{
		LN* ptail = *pphead;
		// 找尾节点进行尾插
		while (ptail->next)
		{
			ptail = ptail->next;
		}
		ptail->next = newln;

	}
}
//打印链表
void printLN(LN* phead)
{
	LN* pucr = phead;
	while (pucr)
	{
		printf("%d->", pucr->val);
		pucr = pucr->next;
	}
	printf("NULL\n");
}

LN* popval(LN* head, int val)
{
	LN* newhead, * newtail;
	newhead = newtail = NULL;
	LN* pcur = head;
	while (pcur)
	{
		if (pcur->val != val)
		{
			if (newhead == NULL)
			{
				newhead = newtail = pcur;
			}
			else
			{
				newtail->next = pcur;
				newtail = newtail->next;
			}
		}
		pcur = pcur->next;
	}
	if (newtail)
		newtail->next = NULL;
	return newhead;
}

int main()
{
	LN* p = NULL;
	Pushback(&p, 1);
	Pushback(&p, 2);
	Pushback(&p, 6);
	Pushback(&p, 3);
	Pushback(&p, 4);
	Pushback(&p, 5);
	Pushback(&p, 6);

	p = popval(p, 6);
	printLN(p);
	return 0;
}